local dushi = fk.CreateSkill {

  name = "joy__dushi",

  tags = { Skill.Compulsory, },

}



dushi:addEffect(fk.AfterDying, {
  name = "joy__dushi",
  anim_type = "negative",
  frequency = Skill.Compulsory,
  events = {fk.AfterDying,fk.Death},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self, false, true)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:handleAddLoseSkills(player,"-"..dushi.name)
    local targets = table.map(table.filter(room:getAlivePlayers(), function(p)
      return not p:hasSkill(dushi.name) end), Util.IdMapper)
    if #targets == 0 then return end
    local to = room:askForChoosePlayers(player, targets, 1, 1, "#joy__dushi-choose", dushi.name, false)
    if #to > 0 then
      to = to[1]
    else
      to = table.random(targets)
    end
    room:handleAddLoseSkills(room:getPlayerById(to), dushi.name, nil, true, false)
  end,
})
dushi:addEffect(fk.Death, {
  name = "joy__dushi",
  anim_type = "negative",
  frequency = Skill.Compulsory,
  events = {fk.AfterDying,fk.Death},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self, false, true)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:handleAddLoseSkills(player,"-"..dushi.name)
    local targets = table.map(table.filter(room:getAlivePlayers(), function(p)
      return not p:hasSkill(dushi.name) end), Util.IdMapper)
    if #targets == 0 then return end
    local to = room:askForChoosePlayers(player, targets, 1, 1, "#joy__dushi-choose", dushi.name, false)
    if #to > 0 then
      to = to[1]
    else
      to = table.random(targets)
    end
    room:handleAddLoseSkills(room:getPlayerById(to), dushi.name, nil, true, false)
  end,
})
dushi:addEffect(fk.EnterDying, {
  refresh_events = {fk.EnterDying},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(dushi.name)
  end,
  on_refresh = function(self, event, target, player, data)
    player:broadcastSkillInvoke(dushi.name)
    player.room:notifySkillInvoked(player, dushi.name)
  end,
})

dushi:addEffect("prohibit", {
  name = "#joy__dushi_prohibit",
  prohibit_use = function(self, player, card)
    if card.name == "peach" and not player.dying then
      return table.find(Fk:currentRoom().alive_players, function(p) return p.dying and p:hasSkill("joy__dushi") and p ~= player end)
    end
  end,
})

return dushi